package com.hy.common.excel;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.common.consts.CommonConst;
import com.hy.common.consts.StateConst;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.MyObjectUtils;
import com.hy.common.utils.StateUtil;
import com.hy.entity.dto.DepartmentSearchDTO;
import com.hy.entity.dto.DetectDetailSearchDTO;
import com.hy.entity.pojo.Detect;
import com.hy.entity.pojo.DetectDetail;
import com.hy.entity.pojo.HyDepartment;
import com.hy.entity.vo.DetectDetailVO;
import com.hy.mapper.DetectDetailMapper;
import com.hy.mapper.DetectMapper;
import com.hy.mapper.HyDepartmentMapper;
import com.hy.service.HyDepartmentService;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Component
public class DetectDetailExport extends AbstractExcelExport<DetectDetailVO>{


    private Integer count;

    @Resource
    private DetectDetailMapper detectDetailMapper;
    @Resource
    private DetectMapper detectMapper;
    public void exportWithBigData(HttpServletResponse response, String fileName){
        List<String> roles = CommonUtils.getCurrentUser().getRoles();
        List<Detect> detects = detectMapper.selectList(new LambdaQueryWrapper<Detect>().and(wapper -> {
            wapper.eq(Detect::getUnitId, CommonUtils.getCurrentUser().getUnitId());
        }));
        this.count=detectDetailMapper.selectCount(new LambdaQueryWrapper<DetectDetail>().and(wapper->
                {        wapper
                        .ne(DetectDetail::getState, StateConst.DETECT_DETAIL_SURE);
                    if(CommonUtils.isMember(roles, CommonConst.SYSTEM_ROLE_NAME)){

                    }else {
                        wapper.eq(DetectDetail::getUnitId,CommonUtils.getCurrentUser().getUnitId());
                        wapper.eq(DetectDetail::getBelongDetectId,detects.get(0).getDetectId());
                    }
                }
        )).intValue();
        this.exportWithBigData(response, fileName, null, null, DetectDetailVO.class);
    }


    //计算出导出的数量
    @Override
    protected Integer dataTotalCount(Map<String, Object> queryCondition) {
        return this.count;
    }

    /**
     * 每一个sheet存放的数据总数
     * @return
     */
    @Override
    protected Integer eachSheetTotalCount() {//每一个sheet存放的数据总数
        return 500000;
    }

    @Override
    protected Integer eachTimesWriteSheetTotalCount() {//每次写入sheet的总数
        return 5000;
    }

    @Override
    protected void buildDataList(List<DetectDetailVO> resultList, Map<String, Object> queryCondition, Integer pageNo, Integer pageSize) {
        List<Detect> detects = detectMapper.selectList(new LambdaQueryWrapper<Detect>().and(wapper -> {
            wapper.eq(Detect::getUnitId, CommonUtils.getCurrentUser().getUnitId());
        }));
        DetectDetailSearchDTO detectDetailSearchDTO = new DetectDetailSearchDTO();
        detectDetailSearchDTO.setNoState(1);
        detectDetailSearchDTO.setUnitId(CommonUtils.getCurrentUser().getUnitId());
        detectDetailSearchDTO.setBelongDetectId(detects.get(0).getDetectId());
        List<DetectDetailVO> detailsByDTO = detectDetailMapper.getDetailsByDTO(null, detectDetailSearchDTO);
        detailsByDTO.forEach(e->{
                e.setStateName(StateUtil.getDetectDetail(e.getState()));
                e.setPhoto(MyObjectUtils.isNotBlank(e.getPhoto())?CommonConst.INTER+e.getPhoto():"暂无");
                resultList.add(e);});

        System.out.println("kk");
//        if(CollUtil.isNotEmpty(entityDoList)){
//            entityDoList.forEach(item -> {
//                resultList.add(Arrays.asList(item.getDepartmentId().toString(),item.getDepartmentName(),item.getDepartmentDetail(),
//                        item.getManagerName(),item.getUnit(),item.getCreateDate()
//                ));
//            });
//        }

    }
}
